package cn.app.android.sichuanxiaoke.com.xiaokeapp.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.R;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.utils.ConstantUtils;

/**
 * app内部转账
 */

public class TransferAccountsActivity extends AppCompatActivity {

    @BindView(R.id.UserCardId)
    EditText mUserCardId;
    @BindView(R.id.moneyNumber)
    EditText mmoneyNumber;
    @BindView(R.id.tb)
    Toolbar tb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transfer_accounts);
        ButterKnife.bind(this);
        initUI();
    }

    private void initUI() {
        setSupportActionBar(tb);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        tb.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @OnClick(R.id.submitOrder)
    public void  OnClick(View view){
        String userid = mUserCardId.getText().toString();
        String number = mmoneyNumber.getText().toString();
        if(userid.isEmpty()){
            mUserCardId.setError("账户不能为空");
            return;
        }

        if(number.isEmpty()){
            mmoneyNumber.setError("金额不能为空");
            return;
        }
        SubmitOrder(userid,number);

    }

    public void SubmitOrder(String userid,String number){
        HttpParams params = new HttpParams();
        params.put("type","to_user");
        params.put("amount",number);
        params.put("to_user",userid);

        OkGo.<String>post(ConstantUtils.APP_INSIDE_TRANSFER_ACCOUNTS).params(params).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String body = response.body();
                Log.i("TransferAccounts","OkgoTest--onSuccess--"+body);
                try {
                    JSONObject jsonObject = new JSONObject(body);
                    int status = jsonObject.optInt("status");
                    if(status == 1){
                        Toast.makeText(TransferAccountsActivity.this,"转账成功",Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(TransferAccountsActivity.this,"转账失败",Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Response<String> response) {

                String body = response.body();
                Log.i("TransferAccounts","OkgoTest--onError--"+body);
            }
        });

    }
}
